<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="userid">
      <el-input v-model="dataForm.userid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="身份证明" prop="identification">
      <el-input v-model="dataForm.identification" placeholder="身份证明"></el-input>
    </el-form-item>
    <el-form-item label="银行卡号" prop="bankcard">
      <el-input v-model="dataForm.bankcard" placeholder="银行卡号"></el-input>
    </el-form-item>
    <el-form-item label="银行卡名称" prop="bankcardname">
      <el-input v-model="dataForm.bankcardname" placeholder="银行卡名称"></el-input>
    </el-form-item>
    <el-form-item label="名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="名字"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userid: '',
          identification: '',
          bankcard: '',
          bankcardname: '',
          name: '',
          age: '',
          gender: ''
        },
        dataRule: {
          userid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          identification: [
            { required: true, message: '身份证明不能为空', trigger: 'blur' }
          ],
          bankcard: [
            { required: true, message: '银行卡号不能为空', trigger: 'blur' }
          ],
          bankcardname: [
            { required: true, message: '银行卡名称不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名字不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/userdetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userid = data.userdetail.userid
                this.dataForm.identification = data.userdetail.identification
                this.dataForm.bankcard = data.userdetail.bankcard
                this.dataForm.bankcardname = data.userdetail.bankcardname
                this.dataForm.name = data.userdetail.name
                this.dataForm.age = data.userdetail.age
                this.dataForm.gender = data.userdetail.gender
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/userdetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userid': this.dataForm.userid,
                'identification': this.dataForm.identification,
                'bankcard': this.dataForm.bankcard,
                'bankcardname': this.dataForm.bankcardname,
                'name': this.dataForm.name,
                'age': this.dataForm.age,
                'gender': this.dataForm.gender
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
